<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_topologyValidatorJobService"></title>
    <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
    <style>
        .ol-popup {
            position: absolute;
            top: 10px;
            right: 10px;
        }
    </style>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="width: 100%;height:100%"></div>
    <div id="popup" class="ol-popup" style='width:350px'>
        <div class="panel panel-default">
            <div class='panel-heading'>
                <h3 class='panel-title' data-i18n="resources.title_topologyValidatorJobService"></h3>
            </div>
            <div class='panel-body'>
                <div class='input-group'>
                    <span class='input-group-addon'><span data-i18n="resources.text_sourceDataset"></span><span
                            data-i18n="[title]resources.text_requiredField" style="color: red;"> * </span>　</span>
                    <input id='datasetName' type='text' class='form-control' value='samples_processing_newyorkZone_R' /></div>
                <p></p>
                <div class='input-group'>
                    <span class='input-group-addon' data-i18n="resources.text_topologyValidatorRule"><span data-i18n="[title]resources.text_requiredField"
                            style="color: red;"> * </span></span>
                    <select class='form-control' id='rule' name='rule'>
                        <option value='REGIONNOOVERLAP' data-i18n="resources.btn_regionNoOverlap"></option>
                        <option value='REGIONNOOVERLAPWITH' data-i18n="resources.btn_regionNoOverlapWith"></option>
                        <option value='REGIONCONTAINEDBYREGION' data-i18n="resources.btn_regionContainedByRegion"></option>
                        <option value='REGIONCOVEREDBYREGION' selected data-i18n="resources.btn_regionCoveredByRegion"></option>
                        <option value='LINENOOVERLAP' data-i18n="resources.btn_lineNoOverlap"></option>
                        <option value='LINENOOVERLAPWITH' data-i18n="resources.btn_lineNoOverlapWith"></option>
                        <option value='POINTNOIDENTICAL' data-i18n="resources.btn_pointNoIdentical"></option>
                    </select>
                </div>
                <p></p>
                <div class='input-group' id='datasetTopologyDiv'>
                    <span class='input-group-addon'><span data-i18n="resources.text_topologyValidatorDataset"></span><span
                            data-i18n="[title]resources.text_requiredField" style="color: red;"> * </span>　</span>
                    <input id='datasetTopology' type='text' class='form-control' value='samples_processing_newyorkResidential_R' /></div>
                <p></p>
                <div class='input-group'>
                    <span class='input-group-addon' data-i18n="resources.text_tolerance"></span>
                    <input id='tolerance' type='text' class='form-control' value='0.000001' />
                </div>
                <p></p>
                <div align='right'>
                    <input type='button' id='btn' class='btn btn-primary' data-i18n="[value]resources.text_analyst" />
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" include="bootstrap,jquery,widgets" src="../js/include-web.js"></script>
    <script type="text/javascript">
        var host = window.isLocal ? window.server : "https://iserver.supermap.io";
        var layer,
            processingsUrl = host + "/iserver/services/distributedanalyst/rest/v1/jobs",
            mapURL = host + "/iserver/services/map-world/rest/maps/World",
            map = new ol.Map({
                target: 'map',
                // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
                controls: ol.control.defaults.defaults({
                        attributionOptions: {
                            collapsed: false
                        }
                    })
                    .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
                view: new ol.View({
                    center: [-73.95, 40.75],
                    zoom: 12,
                    projection: 'EPSG:4326',
                    multiWorld: true
                })
            });
        map.addLayer(new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest({
                url: mapURL,
            }),
        }));

        var processingService = new ol.supermap.ProcessingService(processingsUrl, {
            withCredentials: window.isLocal
        });

        initForm();
        bindClick();

        function initForm() {
            $("#rule").change(function () {
                var rule = $(this).val();
                if (rule == "PointNoIdentical" || rule == "LineNoOverlap" || rule == "RegionNoOverlap") {
                    $("#datasetTopologyDiv").hide();
                } else {
                    $("#datasetTopologyDiv").show();
                }
            });
            var info = new ol.control.Control({
                element: popup
            });
            info.setMap(map);
            map.addControl(info);
        }

        function bindClick() {
            $('#btn').on('click', function () {
                if ($('#msg_container')[0]) {
                    $('#msg_container').remove();
                }
                widgets.loader.showLoader();
                if (map && layer) {
                    map.removeLayer(layer);
                }
                TopologyValidatorJobs();
            });
        }
        ol.supermap.SecurityManager.registerToken(processingsUrl, window.exampleToken);

        function TopologyValidatorJobs() {
            var topologyValidatorJobsParameter = new ol.supermap.TopologyValidatorJobsParameter({
                datasetName: $('#datasetName').val(),
                datasetTopology: $('#datasetTopology').val(),
                rule: $('#rule option:selected').attr('value'),
                tolerance: $('#tolerance').val()
            });
            processingService.addTopologyValidatorJob(topologyValidatorJobsParameter).then(function (serviceResult) {
                if (serviceResult.error) {
                    widgets.loader.removeLoader();
                    var errorMsg = serviceResult.error.errorMsg || "code: " + serviceResult.error.code;
                    widgets.alert.showAlert(resources.msg_createFailed + "<br>" + errorMsg, false);
                    return;
                }
                serviceResult.result.setting.serviceInfo.targetServiceInfos.map(function (info) {
                    if (info.serviceType === 'RESTMAP') {
                        ol.supermap.FetchRequest.get(info.serviceAddress + '/maps').then(function (
                            response) {
                            return response.json();
                        }).then(function (result) {
                            var mapUrl = result[0].path;
                            new ol.supermap.MapService(mapUrl).getMapInfo().then(function (mapInfo) {
                                layer = new ol.layer.Tile({
                                    source: new ol.source.ImageSuperMapRest(ol.source
                                        .ImageSuperMapRest.optionsFromMapJSON(
                                            mapUrl, mapInfo.result))
                                });
                                map.addLayer(layer);
                                widgets.loader.removeLoader();
                            });
                        });
                    }
                });
            });
        }
    </script>
</body>

</html>